//
//  ChangeNickNameViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2019/12/2.
//  Copyright © 2019 AAIM. All rights reserved.
//

#import "ChangeNickNameViewController.h"
#import <CoreImage/CoreImage.h>
#import "ContactRequestLogic.h"
@interface ChangeNickNameViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField * inputTextFiled;

//二维码
@property (nonatomic,strong) UIView * QRCodeBackView;//二维码背景
@property (nonatomic,strong) UIButton * saveImgBtn;//保存图片按钮
@property (nonatomic,strong) UIImageView * headerImageView;
@property (nonatomic,strong) UILabel * nickNameLabel;
@property (nonatomic,strong) UIImageView * QRCodeImageView;
@property (nonatomic,strong) UILabel * tipLabel;
@property (nonatomic,strong) UIImageView *ig;//生成海报背景图片

//性别
@property (nonatomic,assign) NSInteger isMan;//0男，1女,2未选
@property (nonatomic,strong) UIButton * sexManBtn;
@property (nonatomic,strong) UIButton * sexWomanBnt;
@property (nonatomic,strong) UILabel * sexLabel;


@end

@implementation ChangeNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.index != 3) {
        // 导航右按钮
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
        [rightBtn sizeToFit];
        [rightBtn setBackgroundColor:KHEXCOLOR(kMainColor)];
        rightBtn.frame = CGRectMake(0, 0, 40, 25);
        rightBtn.layer.cornerRadius = 2;
        rightBtn.layer.masksToBounds = YES;
        rightBtn.titleLabel.font = kMainFont(12);
        rightBtn.jk_touchAreaInsets = UIEdgeInsetsMake(20, 20, 20, 20);
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        self.gk_navigationItem.rightBarButtonItem = rightItem;
    }
    
    if(![_pageType isEqualToString:@"friendInfo"]){
     _model = [LoginManager shareLoginManager].userModel;
    }
    
    //1修改昵称，2签名，3二维码，4性别，5公司，6职业
    switch (self.index) {
        case 1:
            self.gk_navTitle = @"修改昵称";
            
            [self.view addSubview:self.inputTextFiled];
            [self setUI];
            if (_contentStr && ![_contentStr isEqualToString:_model.nickname]) {
               self.inputTextFiled.text = _contentStr;
            }else{
                 self.inputTextFiled.placeholder = _model.nickname;
            }
           
            break;
        case 2:
            self.gk_navTitle = @"签名";
            
            [self.view addSubview:self.inputTextFiled];
            [self setUI];
            if (_contentStr && ![_contentStr isEqualToString:_model.remark]) {
               self.inputTextFiled.text = _contentStr;
            }else{
               
                  self.inputTextFiled.placeholder = @"请简单的介绍下自己";
            
                 
            }
           
            break;
        case 3:
        {
            self.gk_navTitle = @"二维码";
            self.view.backgroundColor = KHEXCOLOR(@"#F4F4F4");
            [self creatQRCode];

        }
            break;
        case 4:
            self.gk_navTitle = @"性别";
            [self changeSex];
            break;
        case 5:
            self.gk_navTitle = @"公司/机构";
            
            [self.view addSubview:self.inputTextFiled];
            [self setUI];
            
            if (_contentStr && ![_contentStr isEqualToString:_model.company]) {
               self.inputTextFiled.text = _contentStr;
            }else{
                    self.inputTextFiled.placeholder = @"输入自己的公司/机构";
            }
            break;
        case 7:
            self.gk_navTitle = @"备注";
            
            [self.view addSubview:self.inputTextFiled];
            [self setUI];
            if (![CommonTools isEmpty:_contentStr]) {
                self.inputTextFiled.text = _contentStr;
            }else if (![CommonTools isEmpty:_model.fremark]){
                self.inputTextFiled.text = _model.fremark;
            } else{
                self.inputTextFiled.placeholder = @"输入备注";
            }
            
            break;
        case 8:
            self.gk_navTitle = @"描述";
            [self.view addSubview:self.inputTextFiled];
            [self setUI];
            if (![_contentStr isEqualToString:@"暂时没有任何描述"]) {
                self.inputTextFiled.text = _contentStr;
            }else if(_model.describe){
                self.inputTextFiled.text = _model.describe;
            }else{
                self.inputTextFiled.placeholder = @"输入描述";
            }
            break;
        default:
            self.gk_navTitle = @"职业";
            
            [self.view addSubview:self.inputTextFiled];
            [self setUI];
            if (_contentStr && ![_contentStr isEqualToString:_model.job]) {
                self.inputTextFiled.text = _contentStr;
            }else{
                self.inputTextFiled.placeholder = @"输入自己的职业";
            }
            break;
    }
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //    if (_index == 3) {
    //        [self showAlwaysTipHudWithText:@"加载中，请稍等"];
    //        NSString * headUrl = _model.head;
    //        dispatch_group_t group = dispatch_group_create();
    //        dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
    //            sleep(1);
    //             dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    //        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",headUrl]] placeholderImage:[UIImage imageNamed:@"bg_pyqtx"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
    //            KWeakSelf;
    //            [weakSelf hideHud];
    //            weakSelf.headerImageView.image = image;
    //            dispatch_semaphore_signal(semaphore);
    //            }];
    //             dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    //        });
    //
    //    }
}

- (void)changeSex{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.sexLabel];
    [self.view addSubview:self.sexManBtn];
    [self.view addSubview:self.sexWomanBnt];
    
    if ([_sexIndex isEqualToString:@"0"]) {
        [_sexManBtn setImage:[UIImage imageNamed:@"选中状态-男"] forState:UIControlStateNormal];
        [_sexWomanBnt setImage:[UIImage imageNamed:@"未选中状态-女"] forState:UIControlStateNormal];
    }else if ([_sexIndex isEqualToString:@"1"]){
        [_sexManBtn setImage:[UIImage imageNamed:@"未选中状态-男"] forState:UIControlStateNormal];
        [_sexWomanBnt setImage:[UIImage imageNamed:@"选中状态-女"] forState:UIControlStateNormal];
    }else{
        [_sexManBtn setImage:[UIImage imageNamed:@"未选中状态-男"] forState:UIControlStateNormal];
        [_sexWomanBnt setImage:[UIImage imageNamed:@"未选中状态-女"] forState:UIControlStateNormal];
    }
    
    [_sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(15.f));
        make.top.equalTo(self.view.mas_top).offset(kTopHeight+20);
        make.width.offset(SCALAE_WIDTH(53.f));
    }];
    [_sexManBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sexLabel.mas_right);
        make.centerY.equalTo(self.sexLabel.mas_centerY);
        make.width.offset(SCALAE_WIDTH(86.f));
    }];
    [_sexWomanBnt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sexManBtn.mas_right).offset(SCALAE_WIDTH(78.f));
        make.centerY.equalTo(self.sexLabel.mas_centerY);
        make.width.offset(SCALAE_WIDTH(86.f));
    }];
    UIView * line = [UIView new];
    line.backgroundColor = KHEXCOLOR(IM_Color_SeparateLine);
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(15.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-15.f));
        make.top.equalTo(self.sexManBtn.mas_bottom).offset(8.f);
        make.height.offset(0.5f);
    }];
    
}
- (void)setWoManActive:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"选中状态-女"] forState:UIControlStateNormal];
        _sexManBtn.selected = NO;
        _isMan = 1;
        [_sexManBtn setImage:[UIImage imageNamed:@"未选中状态-男"] forState:UIControlStateNormal];
    }else{
        [sender setImage:[UIImage imageNamed:@"未选中状态-女"] forState:UIControlStateNormal];
        _isMan = 2;
    }
}

- (void)setManActive:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"选中状态-男"] forState:UIControlStateNormal];
        _sexWomanBnt.selected = NO;
        _isMan = 0;
        [_sexWomanBnt setImage:[UIImage imageNamed:@"未选中状态-女"] forState:UIControlStateNormal];
    }else{
        [sender setImage:[UIImage imageNamed:@"未选中状态-男"] forState:UIControlStateNormal];
        _isMan = 2;
    }
}


- (void)setUI{
    [_inputTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(15.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-15.f));
        make.top.equalTo(self.view.mas_top).offset(kTopHeight);
        make.height.offset(43.f);
    }];
    UIView * line = [UIView new];
    line.backgroundColor = KHEXCOLOR(IM_Color_SeparateLine);
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(15.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-15.f));
        make.top.equalTo(self.inputTextFiled.mas_bottom);
        make.height.offset(0.5f);
    }];
}


- (void)creatQRCode{
    [self.view addSubview:self.QRCodeBackView];
    [self.view addSubview:self.saveImgBtn];
    
    [_QRCodeBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(29.f));
        make.top.equalTo(self.view.mas_top).offset(31.f+kTopHeight);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-29.f));
        make.height.offset(450.f);
    }];
    
    [_saveImgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.QRCodeBackView.mas_bottom).offset(26.f);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(SCALAE_WIDTH(75.f), SCALAE_WIDTH(55.f)));
    }];
    //一般的view生成图片方法
    
    KWeakSelf;
    [self showAlwaysTipHudWithText:@"加载中，请稍等"];
    NSString * headUrl = _model.head;
    if ([_pageType isEqualToString:@"friendInfo"]) {
        headUrl = [NSString stringWithFormat:@"%@%@",userHeaderImage,_model.head];
    }
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        sleep(1);
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.headerImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",headUrl]] placeholderImage:[UIImage imageNamed:@"bg_pyqtx"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

                //回调或者说是通知主线程刷新，
                weakSelf.headerImageView.image = image;
                [weakSelf.QRCodeImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userInfoImage,weakSelf.qrCodeStr]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                     [weakSelf hideHud];
                    weakSelf.QRCodeImageView.image = image;
                    dispatch_semaphore_signal(semaphore);
                }];

                
            }];
        });
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    });
    
    
    dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"任务2开始执行");
            weakSelf.ig=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCALAE_WIDTH(317.f), 450.f)];
            weakSelf.ig.image = [weakSelf onUIImageSubViewWithView:weakSelf.QRCodeBackView];
            [weakSelf.ig setContentScaleFactor:[[UIScreen mainScreen] scale]];
            weakSelf.ig.contentMode =  UIViewContentModeScaleAspectFit;
            [weakSelf.QRCodeBackView addSubview:weakSelf.ig];
        });
    });
    
}

////生成专属二维码
//- (UIImage *)creatUserQRCode{
//    // 1. 创建一个二维码滤镜实例(CIFilter)
//    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//    // 滤镜恢复默认设置
//    [filter setDefaults];
//
//    // 2. 给滤镜添加数据
//    NSString *string = @"";
//    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
//    // 使用KVC的方式给filter赋值
//    [filter setValue:data forKeyPath:@"inputMessage"];
//
//    // 3. 生成二维码
//    CIImage *image = [filter outputImage];
//
//    //4.在中心增加一张图片
//    UIImage *img = [self createNonInterpolatedUIImageFormCIImage:image withSize:SCALAE_WIDTH(229.f)];
//
//    //5.把中央图片划入二维码里面
//    //5.1开启图形上下文
//    UIGraphicsBeginImageContext(img.size);
//    //5.2将二维码的图片画入
//    [img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
//    UIImage *centerImg = _headerImageView.image;
//    CGFloat centerW=img.size.width*0.3;
//    CGFloat centerH=centerW;
//    CGFloat centerX=(img.size.width-centerW)*0.5;
//    CGFloat centerY=(img.size.height -centerH)*0.5;
//    [centerImg drawInRect:CGRectMake(centerX, centerY, centerW, centerH)];
//    //5.3获取绘制好的图片
//    UIImage *finalImg=UIGraphicsGetImageFromCurrentImageContext();
//    //5.4关闭图像上下文
//    UIGraphicsEndImageContext();
//
//    //6.显示最终二维码
//    //    self.QRCodeImageView.image = finalImg;
//    return finalImg;
//
//}

/**
 *  调用该方法处理图像变清晰
 *  根据CIImage生成指定大小的UIImage
 *
 *  @param image CIImage
 *  @param size  图片宽度以及高度
 */
//- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
//{
//    CGRect extent = CGRectIntegral(image.extent);
//    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
//
//    //1.创建bitmap;
//    size_t width = CGRectGetWidth(extent) * scale;
//    size_t height = CGRectGetHeight(extent) * scale;
//    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
//    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
//    CIContext *context = [CIContext contextWithOptions:nil];
//    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
//    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
//    CGContextScaleCTM(bitmapRef, scale, scale);
//    CGContextDrawImage(bitmapRef, extent, bitmapImage);
//
//    //2.保存bitmap到图片
//    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
//    CGContextRelease(bitmapRef);
//    CGImageRelease(bitmapImage);
//    return [UIImage imageWithCGImage:scaledImage];
//}


//view 需要生成的图片
- (UIImage *)onUIImageSubViewWithView:(UIView *)view
{
    //    //view容器,
    CGFloat width = SCALAE_WIDTH(317.f);
    CGFloat height = 450.f;
    
    //主图
    [view addSubview:self.headerImageView];
    
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.QRCodeBackView.mas_left).offset(SCALAE_WIDTH(24.f));
        make.top.equalTo(self.QRCodeBackView.mas_top).offset(34.f);
        make.size.mas_equalTo(CGSizeMake(SCALAE_WIDTH(69.f), SCALAE_WIDTH(69.f)));
    }];
    [view addSubview:self.nickNameLabel];
    [_nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerImageView.mas_right).offset(SCALAE_WIDTH(10.f));
        make.centerY.equalTo(self.headerImageView.mas_centerY);
        make.width.offset(SCALAE_WIDTH(200.f));
    }];
    [view addSubview:self.QRCodeImageView];
    [_QRCodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.QRCodeBackView.mas_centerX);
        make.top.equalTo(self.headerImageView.mas_bottom).offset(21.f);
        make.size.mas_equalTo(CGSizeMake(SCALAE_WIDTH(229.f), SCALAE_WIDTH(262.f)));
    }];
    
//    [view addSubview:self.tipLabel];
//    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.QRCodeImageView.mas_bottom).offset(10.f);
//        make.centerX.equalTo(self.view.mas_centerX);
//        make.width.offset(SCALAE_WIDTH(200.f));
//    }];
    //生成图片
    return [self makeImageWithView:_QRCodeBackView withSize:CGSizeMake(width, height)];
}

#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@"2--%@",image);
    return image;
    
}

//判断是否全是空格

- (BOOL) isEmpty:(NSString *) str {
    if (!str) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
        
    }
    
}

-(void)changeNicknameActive:(UITextField *)textFiled{
    NSLog(@"修改");
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  // return NO to not change text
{
    
    //    //限制其输入长度
    if (range.length + range.location > textField.text.length) {
        return NO;
    }
    NSUInteger length = textField.text.length + string.length - range.length;
    
    if (_index == 1|| _index == 6) {
        return length <= 10;
        
    }else if(_index == 2){
        return length <= 30;
    }else if(_index == 8){
        return length <= 50;
    }else{
        return length <= 20;
    }
    return YES;
    
}


//保存
- (void)saveClick:(UIButton *)sender{
    
    
    if (self.delegate) {
        if (_isMan == 2) {
            
            [MBProgressHUD showError:@"您没修改任何内容" afterDelay:3];
            return;
        }
        if (_index == 4) {
            [self.delegate changeWithStr:[NSString stringWithFormat:@"%ld",(long)_isMan] withIndexPath:_indexPath];
        }else{
            if(_index != 7){
                if ([_inputTextFiled.text isEqualToString:_inputTextFiled.placeholder]||[self isEmpty:_inputTextFiled.text]) {
                               
                               [MBProgressHUD showError:@"您没修改任何内容" afterDelay:3];
                               return;
                           }
            }
           
            NSLog(@"输入L：%@----%@",_inputTextFiled.text,
                  _indexPath);
            if (_index != 7 && _index != 8) {
                [self.delegate changeWithStr:_inputTextFiled.text withIndexPath:_indexPath];
            }
        }
        if (_index == 7) {
            [self requestChangeRemark];

        }else if (_index == 8) {
            [self requestChangeDescribe];
            
           
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }
}
- (void)requestChangeDescribe{
    
    [ContactRequestLogic requestJoinBlackListFriendWithFuuid:_fuuid withBlock:@"0" withDescribe:_inputTextFiled.text withRemark:_model.remark Success:^(NSDictionary * _Nonnull Dict) {
        KWeakSelf;
        [weakSelf showTipHudWithText:Dict[@"message"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[UserDataBaseTool shareUserDataBaseTool]reloadData:1 Success:^(BOOL isOK) {
                if (isOK) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:kMessageSystemAddFriends object:nil];
                }
            }];
             [weakSelf.delegate changeWithStr:weakSelf.inputTextFiled.text withIndex:weakSelf.index];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}

- (void)requestChangeRemark{
    [ContactRequestLogic requestJoinBlackListFriendWithFuuid:_fuuid withBlock:@"0" withDescribe:_model.describe withRemark:_inputTextFiled.text Success:^(NSDictionary * _Nonnull Dict) {
        KWeakSelf;
        [weakSelf showTipHudWithText:Dict[@"message"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[UserDataBaseTool shareUserDataBaseTool]reloadData:1 Success:^(BOOL isOK) {
                           if (isOK) {
                               [[NSNotificationCenter defaultCenter] postNotificationName:kMessageSystemAddFriends object:nil];
                           }
                       }];
             [weakSelf.delegate changeWithStr:weakSelf.inputTextFiled.text withIndex:weakSelf.index];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}

//保存到相册
- (void)saveImgTouch:(UIButton * )sender{
    [MBProgressHUD showMessage:@"保存中..." toView:self.view];
    UIImageWriteToSavedPhotosAlbum(self.ig.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    [MBProgressHUD hideHUDForView:self.view];
    NSString *message = @"成功";
    if (!error) {
        message = @"成功保存到相册";
        [MBProgressHUD showSuccess:message afterDelay:3];
    }else
    {
        message = [error description];
        [MBProgressHUD showError:@"保存失败" afterDelay:3];
    }
}


- (UITextField *)inputTextFiled{
    if (!_inputTextFiled) {
        _inputTextFiled = [[UITextField alloc]init];
        _inputTextFiled.font = kMainFont(15);
        _inputTextFiled.delegate =self;
        _inputTextFiled.textColor = KHEXCOLOR(@"#262B2F");
        _inputTextFiled.borderStyle = UITextBorderStyleNone;
        [_inputTextFiled addTarget:self action:@selector(changeNicknameActive:) forControlEvents:UIControlEventEditingChanged];
    }
    return _inputTextFiled;
}

- (UIView *)QRCodeBackView{
    if (!_QRCodeBackView) {
        _QRCodeBackView = [[UIView alloc]init];
        _QRCodeBackView.backgroundColor = [UIColor whiteColor];
        _QRCodeBackView.layer.cornerRadius = 5;
        _QRCodeBackView.layer.masksToBounds = YES;
    }
    return _QRCodeBackView;
}

-(UIButton *)saveImgBtn{
    if (!_saveImgBtn) {
        _saveImgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveImgBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveImgBtn setImage:[UIImage imageNamed:@"保存按钮"] forState:UIControlStateNormal];
        [_saveImgBtn setTitleEdgeInsets:UIEdgeInsetsMake(15, -18, -15, 18)];
        [_saveImgBtn setImageEdgeInsets:UIEdgeInsetsMake(-15, 15, 15, -15)];
        [_saveImgBtn addTarget:self action:@selector(saveImgTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_saveImgBtn setTitleColor:KHEXCOLOR(@"#AFAFAF") forState:UIControlStateNormal];
        _saveImgBtn.titleLabel.font = kMainFont(15);
    }
    return _saveImgBtn;
}

- (UIImageView *)headerImageView{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc]init];
        _headerImageView.layer.cornerRadius = SCALAE_WIDTH(69.f)/2;
        _headerImageView.layer.masksToBounds = YES;
    }
    return _headerImageView;
}

-(UILabel *)nickNameLabel{
    if (!_nickNameLabel) {
        _nickNameLabel = [[UILabel alloc]init];
        _nickNameLabel.textColor = KHEXCOLOR(@"#262B2F");
        _nickNameLabel.font = kFont(18.f);
        _nickNameLabel.text = _model.nickname;
    }
    return _nickNameLabel;
}

-(UIImageView *)QRCodeImageView{
    if (!_QRCodeImageView) {
        _QRCodeImageView = [[UIImageView alloc]init];
//        _QRCodeImageView.image = [self creatUserQRCode];
    }
    return _QRCodeImageView;
}

-(UILabel *)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc]init];
        _tipLabel.textColor = KHEXCOLOR(@"#262B2F");
        _tipLabel.font = kMainFont(12.f);
        _tipLabel.text = @"扫一扫，加好友";
        _tipLabel.textAlignment = 1;
    }
    return _tipLabel;
}

-(UIButton *)sexManBtn{
    if (!_sexManBtn) {
        _sexManBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sexManBtn setTitle:@"男" forState:UIControlStateNormal];
        [_sexManBtn setImage:[UIImage imageNamed:@"未选中状态-男"] forState:UIControlStateNormal];
        [_sexManBtn setBackgroundColor:KHEXCOLOR(@"#FFFFFF")];
        [_sexManBtn setTitleColor:KHEXCOLOR(@"#AFAFAF") forState:UIControlStateNormal];
        [_sexManBtn addTarget:self action:@selector(setManActive:) forControlEvents:UIControlEventTouchUpInside];
        [_sexManBtn.titleLabel setFont:kMainFont(15)];
        [_sexManBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 15.f, 0, -15.f)];
    }
    return _sexManBtn;
    
}

-(UIButton *)sexWomanBnt{
    if (!_sexWomanBnt) {
        _sexWomanBnt = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sexWomanBnt setTitle:@"女" forState:UIControlStateNormal];
        [_sexWomanBnt setImage:[UIImage imageNamed:@"未选中状态-女"] forState:UIControlStateNormal];
        [_sexWomanBnt setBackgroundColor:KHEXCOLOR(@"#FFFFFF")];
        [_sexWomanBnt setTitleColor:KHEXCOLOR(@"#AFAFAF") forState:UIControlStateNormal];
        [_sexWomanBnt addTarget:self action:@selector(setWoManActive:) forControlEvents:UIControlEventTouchUpInside];
        [_sexWomanBnt.titleLabel setFont:kMainFont(15)];
        [_sexWomanBnt setTitleEdgeInsets:UIEdgeInsetsMake(0, 15.f, 0, -15.f)];
    }
    return _sexWomanBnt;
    
}

-(UILabel *)sexLabel{
    if (!_sexLabel) {
        _sexLabel = [[UILabel alloc]init];
        _sexLabel.text = @"性别:";
        _sexLabel.font = kMainFont(15.f);
        _sexLabel.textColor = KHEXCOLOR(@"#000000");
    }
    return _sexLabel;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
